package com.demomaster.quickcompat.util

class StringUtil {
    companion object {
        @JvmStatic
        fun isNotEmpty(str: String?): Boolean {
            return !str.isNullOrBlank() && str.trim().toLowerCase() != "null"
        }
        @JvmStatic
        fun isLowerCaseLetter(str: String?): Boolean {
            // 检查字符串是否为null或者为空
            if (str.isNullOrBlank()) {
                return false
            }
            // 检查每个字符是否都是小写字母
            return str.all { it in 'a'..'z' }
        }
        @JvmStatic
        fun parseDouble(str: String?): Double {
            return parseDouble(str, 0.0)
        }
        @JvmStatic
        fun parseDouble(str: String?, defValue: Double): Double {
            return try {
                str?.toDouble() ?: defValue
            } catch (e: NumberFormatException) {
                e.printStackTrace()
                defValue
            }
        }
        @JvmStatic
        fun isUpperCaseLetter(str: String?): Boolean {
            // 如果字符串是null或空白，返回false
            if (str.isNullOrBlank()) {
                return false
            }
            // 检查每个字符是否都是大写字母
            return str.all { it in 'A'..'Z' }
        }
        @JvmStatic
        fun getRandomString(length: Int): String {
            val charPool : List<Char> = ('a'..'z') + ('A'..'Z') + ('0'..'9')
            return (1..length)
                .map { _ -> charPool.random() }
                .joinToString("")
        }
        @JvmStatic
        fun str2HexStr(str: String): String {
            val hexChars = "0123456789ABCDEF"
            val bytes = str.toByteArray()
            val result = StringBuilder()

            for (byte in bytes) {
                val highNibble = byte.toInt() shr 4 and 0x0F // 获取高四位
                val lowNibble = byte.toInt() and 0x0F         // 获取低四位
                result.append(hexChars[highNibble])
                result.append(hexChars[lowNibble])
                result.append(' ') // 在每个字节后添加空格
            }

            return result.toString().trim() // 移除末尾多余的空格
        }
    }
}